import Vue from 'vue'
import VueRouter from 'vue-router'
import Cookies from 'js-cookie'
import store from '../store/index'
import {select_user_by_username,select_Friend} from "../request/api";

Vue.use(VueRouter).use(Cookies)

const routes = [
  {
    path:'/login',
    component:() => import('@/views/login'),
  },

  {
    path:'/register',
    component:() => import('@/views/register'),
  },

  {
    path:'/center',
    component:() => import('@/views/center'),
    children:[
        {
            path:'message',
            component:() => import('@/views/center/message')
        },
        {
            path:'friend',
            component:() => import('@/views/center/friend')
        },
        {
            path:'wode',
            component:() => import('@/views/center/wode')
        },
        {
            path:'addFriend',
            component:() => import('@/views/center/addFriend')
        },
        {
            path:'acceptFriend',
            component:() => import('@/views/center/acceptFriend')
        },
        {
            path:'addFriend_detail',
            component:() => import('@/views/center/addFriend_detail')
        },
        {
            path:'friend_detail',
            component:() => import('@/views/center/friend_detail')
        },
        {
            path:'chat',
            component:() => import('@/views/center/chat')
        },
        {
            path:'*',
            redirect:'friend',
        }
    ],
  },

  {
    path:'*',
    redirect:'/login',
  }
]

const router = new VueRouter({
  routes
})

//路由拦截
router.beforeEach((to,from,next)=>{
    let register_and_login=['/login','/register'];
    if(register_and_login.includes(to.fullPath)){
        next();
    }else{
          let username=Cookies.get('username');
          let password=Cookies.get('password');
          if((username === undefined) || (password === undefined)){
              next('/login');
          }else{

              if(store.state.websocket === null){

                  store.commit("setWebSocket",new WebSocket("ws://120.78.80.190:8282"));

                  //当连上socket服务器时绑定username
                  store.state.websocket.onopen = () => {
                      let message={
                          "username":Cookies.get('username'),
                          "type":"bind",
                      }
                      store.state.websocket.send(JSON.stringify(message));
                  }
              }

              //如果某一个页面没有用户列表则请求用户列表
              if(store.state.userList.length === 0){
                  store.dispatch("requertUserList");
              }

              if(store.state.myselfAttribute === {}){
                  //查询自身信息 存入状态管理
                  select_user_by_username({"username":Cookies.get('username')}).then(res=>{
                      store.commit("setMySelfAttribute",res.data);

                      //查询好友列表 存入状态管理
                      let myid={
                          "fromid":store.state.myselfAttribute.username,
                      }
                      select_Friend(myid).then(res=>{
                          if(res.data.type !== "null"){
                              store.commit("setFriendList",res.data);
                          }
                      })

                  })
              }

              next();
          }
    }
})

export default router
